package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.MesEquipment;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.MesEquipmentDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2022-03-30 15:25:04
 */
public class MesEquipmentConverts {

    private static Logger LOG = LoggerFactory.getLogger(MesEquipmentConverts.class);

    public static List<MesEquipmentDTO> convert(List<MesEquipment> mesEquipmentList) {
        if (CollectionUtils.isEmpty(mesEquipmentList)) {
            return Collections.EMPTY_LIST;
        }

        List<MesEquipmentDTO> mesEquipmentListResult = Lists.newArrayList();
        for (MesEquipment mesEquipment : mesEquipmentList) {
            mesEquipmentListResult.add(convert(mesEquipment));
        }
        return mesEquipmentListResult;
    }

    public static MesEquipmentDTO convert(MesEquipment mesEquipment) {
        try {
            return BeanUtils.copy(mesEquipment, MesEquipmentDTO.class);
        } catch (Exception e) {
            LOG.error("MesEquipment到MesEquipmentDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "MesEquipment到MesEquipmentDTO转换失败", e);
        }
    }

    public static MesEquipment convert(MesEquipmentDTO mesEquipmentDTO) {
        try {
            return BeanUtils.copy(mesEquipmentDTO, MesEquipment.class);
        } catch (Exception e) {
            LOG.error("MesEquipmentDTO到MesEquipment转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "MesEquipmentDTO到MesEquipment转换失败", e);
        }
    }
}